﻿using System.ComponentModel;
using System.ComponentModel.Composition;
using System.IO;
using System.Reflection;
using System.Threading.Tasks;
using NthDownload.Contracts.Plugins.Downloads;
using NthDownload.DownloadPlugins.Http.Persistence;
using NthDownload.DownloadPlugins.Http.Serialization;

namespace NthDownload.DownloadPlugins.Http
{
    [PartCreationPolicy(CreationPolicy.Shared)]
    [Export(typeof(IDownloadPlugin))]
    public class HttpDownloadPlugin : IDownloadPlugin
    {
        #region INotifyPropertyChanged

        public event PropertyChangedEventHandler PropertyChanged;

        protected void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        #endregion INotifyPropertyChanged

        #region Class Members

        private bool _isSelected;

        #endregion Class Members

        #region Properties

        /// <summary>
        /// GUID
        /// </summary>
        public string GUID
        {
            get
            {
                return "1234";
            }
        }

        /// <summary>
        /// Name of the plugin
        /// </summary>
        public string Name
        {
            get
            {
                return "Http Download";
            }
        }

        /// <summary>
        /// Description of the plugin
        /// </summary>
        public string Description
        {
            get
            {
                return "Provides the ability to download over Http ";
            }
        }

        /// <summary>
        /// Name used for display
        /// </summary>
        public string DisplayName
        {
            get
            {
                return "Http";
            }
        }

        /// <summary>
        /// Used to indicate if the download is selected
        /// </summary>
        public bool IsSelected
        {
            get
            {
                return _isSelected;
            }
            set
            {
                _isSelected = value;
                OnPropertyChanged("IsSelected");
            }
        }

        /// <summary>
        /// Indicates if this plugin has settings
        /// </summary>
        public bool HasSettings
        {
            get
            {
                return true;
            }
        }

        /// <summary>
        /// Settings
        /// </summary>
        [Import(AllowRecomposition = true)]
        public HttpSettingsPersistenceService SettingsPersistenceService { get; set; }

        #endregion Properties

        #region Methods

        public IDownloadFactory CreateDownloadFactory()
        {
            return new HttpDownloadFactory(GUID);
        }

        public IDownloadUIService CreateDownloadUIService(IDownloadFactory factory)
        {
            return new HttpDownloadUIService(factory);
        }

        public IDownloadXmlSerializationService CreateDownloadXmlSerializationService()
        {
            return new HttpDownloadXmlSerializationService(GUID);
        }

        public Task Initialize()
        {
            Task loadSettingsTask = SettingsPersistenceService.Load();

            Task loadDependencies = loadSettingsTask.ContinueWith(t =>
            {
                //
                // Load dependencies
                //
                DirectoryInfo di = new DirectoryInfo(GetType().Assembly.Location + ".Dependencies");
                if (!di.Exists) return;

                foreach (FileInfo fi in di.GetFiles())
                {
                    Assembly.LoadFrom(fi.FullName);        
                }                
            });

            return loadDependencies;
        }

        #endregion Methods
    }
}
